//
//  HomeViewController.m
//  DanTang
//
//  Created by honeyrr on 2017/9/4.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "HomeViewController.h"
#import "SearchViewController.h"
#import "DTHTableViewController.h"


@interface HomeViewController ()<UIScrollViewDelegate>

//标签数组
@property(nonatomic, strong) NSMutableArray *channels;

//头部视图
@property(nonatomic, strong) UIView *titleView;

//选中的按钮
@property(nonatomic,strong) UIButton *selectBtn;

//red指示器
@property(nonatomic, strong) UIView *redView;

//内容scrollview
@property(nonatomic, strong) UIScrollView *contentView;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarItem];
    
    [self loadHeaderInfo];
}

//请求头部数据
- (void)loadHeaderInfo
{
    self.channels = [NSMutableArray new];
    NSString *urlString = @"http://api.dantangapp.com/v2/channels/preset?gender=1&generation=1";
    [[DTNetWork sharedNetWork] loadDataInfo:urlString parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        [self.channels addObjectsFromArray:responseObject[@"data"][@"channels"]];
        [self setUpTitle];
        //调用首页内容页
        [self setContentView];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void) setUpTitle
{
    //标签整体
    self.titleView = [UIView new];
    self.titleView.backgroundColor = MRRGBColor(240, 240, 240);
    self.titleView.mr_y = 64;
    self.titleView.mr_width = self.view.mr_width;
    self.titleView.mr_height = 36;
    [self.view addSubview:self.titleView];
    
    
    //底部红色光标(指示器)
    self.redView = [UIView new];
    self.redView.backgroundColor = MRNavTabRGBColor;
    self.redView.mr_height = 2;
    self.redView.mr_y = self.titleView.mr_height - self.redView.mr_height;
    [self.titleView addSubview:self.redView];
    
    //内部标签
    CGFloat width = self.view.mr_width / self.channels.count;
    CGFloat height = self.titleView.mr_height - 2;
    
    for (NSInteger i = 0; i < self.channels.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * width, 0, width, height)];
        [button setTitle:self.channels[i][@"name"] forState:UIControlStateNormal];
        button.tag = i;
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:MRNavTabRGBColor forState:UIControlStateDisabled];
        
        //调用创建tableview
        [self addChildVC:self.channels[i]];
        
        [self.titleView addSubview:button];
        
        [button addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            //设置button光标
            button.enabled = NO;
            //文字自动适应尺寸
            [button.titleLabel sizeToFit];
            self.selectBtn = button;

            self.redView.mr_width = button.titleLabel.mr_width;
            self.redView.mr_centerX = button.mr_centerX;
            
            [self headerBtnClick:button];
        }
    }
}

//创建子视图控制器
-(void)addChildVC:(NSDictionary *)dict
{
    DTHTableViewController *dthTV = [[DTHTableViewController alloc]init];
    dthTV.channesID = [dict[@"id"] integerValue];
    [self addChildViewController:dthTV];
}

-(void)headerBtnClick:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    self.selectBtn.enabled = YES;
    sender.enabled = NO;
    
    self.selectBtn = sender;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.redView.mr_width = sender.titleLabel.mr_width;
        self.redView.mr_centerX = sender.mr_centerX;
    }];
    
    //滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = tag * self.contentView.mr_width;
    //设置contentview的位置
    [self.contentView setContentOffset:offset animated:YES];
}

//初始化scrollview的内容页
-(void)setContentView
{
    //取消自动设置scrollview的Insets
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *sv = [[UIScrollView alloc]init];
    sv.frame = self.view.bounds;
    sv.delegate = self;
    //设置背景颜色，以及透明度
    sv.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2];
    sv.pagingEnabled = YES;
    sv.bounces = NO;
    sv.showsHorizontalScrollIndicator = NO;
    sv.contentSize = CGSizeMake(self.channels.count * self.view.mr_width, 0);
    [self.view insertSubview:sv belowSubview:self.titleView];
    self.contentView = sv;
    
    [self scrollViewDidEndScrollingAnimation:sv];
}

#pragma mark - <uiscrollviewdelegate>
// 当每次scrollView切换的时候就会调用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    CGFloat width = scrollView.mr_width;
    //根据滑动的坐标拿到下标
    NSInteger index = scrollView.contentOffset.x / width;
    //拿到对应下标控制器
    UITableViewController *willShow = self.childViewControllers[index];
    //如果这个TableView对象加载过了，则直接返回
    if ([willShow isViewLoaded]) {
        return;
    }
    willShow.view.mr_x = index * width;
    willShow.view.mr_y = 0;
    willShow.view.mr_height = self.view.mr_height;
    [scrollView addSubview:willShow.view];
}

// 当手指拖动scrollView滑动的时候调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:self.contentView];
    //获得宽
    CGFloat width = scrollView.mr_width;
    //计算下标
    NSInteger index = scrollView.contentOffset.x / width;
    UIButton *button = self.titleView.subviews[index + 1];
    [self headerBtnClick:button];
}

-(void) setNavBarItem
{
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Feed_SearchBtn_18x18_"] style:UIBarButtonItemStylePlain target:self action:@selector(searchClick)];
    self.navigationItem.rightBarButtonItem = searchItem;
}

-(void)searchClick
{
    SearchViewController *searchVC = [SearchViewController new];
    [self.navigationController pushViewController:searchVC animated:YES];
}

@end
